import type { Settings as LayoutSettings } from '@ant-design/pro-layout';
import { SettingDrawer } from '@ant-design/pro-layout';
import { PageLoading } from '@ant-design/pro-layout';
import type { RunTimeLayoutConfig } from 'umi';
import { history, Link } from 'umi';
import React from 'react';
import * as allIcons from '@ant-design/icons';
import RightContent from '@/components/RightContent';
import Footer from '@/components/Footer';
import { currentUser as queryCurrentUser } from './services/ant-design-pro/api';
import { message, Modal, notification } from 'antd';
import defaultSettings from '../config/defaultSettings';
import { RequestConfig } from 'umi';

const isDev = process.env.NODE_ENV === 'development';
const loginPath = '/user/login';

/** 获取用户信息比较慢的时候会展示一个 loading */
export const initialStateConfig = {
    loading: <PageLoading />,
};

/**
 * @see  https://umijs.org/zh-CN/plugins/plugin-initial-state
 * */
export async function getInitialState(): Promise<{
    settings?: Partial<LayoutSettings>;
    currentUser?: API.CurrentUser;
    loading?: boolean;
    fetchUserInfo?: () => Promise<API.CurrentUser | undefined>;
}> {
    const fetchUserInfo = async () => {
        try {
            const msg = await queryCurrentUser();
            return msg.data;
        } catch (error) {
            console.log('error', loginPath);
            history.push(loginPath);
        }
        return undefined;
    };
    console.log('history.location.pathname', history.location.pathname);
    // 如果不是登录页面，执行
    if (history.location.pathname !== loginPath) {
        const currentUser = await fetchUserInfo();
        return {
            fetchUserInfo,
            currentUser,
            settings: defaultSettings,
        };
    }
    return {
        fetchUserInfo,
        settings: defaultSettings,
    };
}

const loopMenuItem = (menus) => {
    // 调用自己写的请求菜单
    // const menus1 = currentUser.menus;

    // 合并系统路由 & 业务菜单
    //const freshMenu = SystemRoutes.concat(menu);
    //console.log('freshMenu', menus);
    const rootMenus = new Array();
    menus.map((item) => {
        const icon = item.icon;
        if (typeof icon === 'string') {
            let icons = icon.split('-');
            let fixIconName = '';
            icons.map((item) => {
                fixIconName += item.slice(0, 1).toLocaleUpperCase() + item.slice(1);
            });
            // console.log('icons', icons);
            fixIconName += 'Outlined';
            // console.log('fixIconName', fixIconName);
            item.icon = React.createElement(allIcons[fixIconName] || allIcons[icon]);
        }
        rootMenus.push(item);
    });
    //console.log('rootMenus', rootMenus);
    return rootMenus;
};

const adminRoutes = [
    {
        path: '/user',
        layout: false,
        routes: [
            {
                path: '/user/login',
                layout: false,
                name: 'login',
                component: './user/Login',
            },
            {
                path: '/user',
                redirect: '/user/login',
            },
            {
                path: '/user/wxlogin',
                layout: false,
                name: 'wxlogin',
                component: './user/wxlogin',
            },
            {
                name: 'register-result',
                icon: 'smile',
                path: '/user/register-result',
                component: './user/register-result',
            },
            {
                name: 'register',
                icon: 'smile',
                path: '/user/register',
                component: './user/register',
            },
            {
                component: '404',
            },
        ],
    },
    {
        path: '/welcome',
        name: 'welcome',
        icon: 'smile',
        component: './Welcome',
    },
    {
        path: '/operations',
        name: 'operations',
        icon: 'profile',
        routes: [
            {
                path: '/operations',
                redirect: '/operations/live',
            },
            {
                name: 'speciallist',
                icon: 'smile',
                path: '/operations/special',
                component: './operations/special/index',
            },
            {
                name: 'specialdetail',
                icon: 'smile',
                hideInMenu: true,
                parentKeys: ['/operations/special'],
                path: '/operations/special-detail',
                component: './operations/special/detail',
            },
            {
                name: 'live',
                icon: 'smile',
                path: '/operations/live',
                component: './operations/live/index',
            },
            {
                name: 'livedetail',
                icon: 'smile',
                hideInMenu: true,
                path: '/operations/live-detail',
                parentKeys: ['/operations/live'],
                component: './operations/live/detail',
            },
        ],
    },
    {
        path: '/doctor',
        name: 'doctor',
        icon: 'profile',
        routes: [
            {
                name: 'authdetail',
                icon: 'smile',
                path: '/doctor/auth-detail',
                component: './doctor/auth-detail',
            },
        ],
    },
    //     {
    //         path: '/hospital',
    //         icon: 'profile',
    //         name: 'hospital',
    //         routes: [
    //             {
    //                 path: '/hospital',
    //                 redirect: '/hospital/hospital-list',
    //             },
    //             {
    //                 name: 'list',
    //                 icon: 'smile',
    //                 path: '/hospital/hospital-list',
    //                 component: './hospital/index',
    //             },
    //             {
    //                 name: 'authlist',
    //                 icon: 'smile',
    //                 path: '/hospital/authList',
    //                 component: './hospital/authList',
    //             },
    //             {
    //                 name: 'authdetail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/hospital/auth-detail',
    //                 parentKeys: ['/hospital/authList'],
    //                 component: './hospital/auth-detail',
    //             },
    //             {
    //                 name: 'detail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/hospital/hospital-detail',
    //                 parentKeys: ['/hospital/hospital-list'],
    //                 component: './hospital/detail',
    //             },
    //             {
    //                 name: 'hospitalDeptList',
    //                 icon: 'smile',
    //                 path: '/hospital/hospitalDept-list',
    //                 component: './hospitalDept/index',
    //             },
    //             {
    //                 name: 'hospitalDeptDetail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/hospital/hospitalDept-detail',
    //                 component: './hospitalDept/detail',
    //                 parentKeys: ['/hospital/hospitalDept-list'],
    //             },
    //             {
    //                 name: 'hospitalNoticeList',
    //                 icon: 'smile',
    //                 path: '/hospital/hospitalNotice-list',
    //                 component: './hospitalNotice/index',
    //             },
    //             {
    //                 name: 'hospitalNoticeDetail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/hospital/hospitalNotice-detail',
    //                 component: './hospitalNotice/detail',
    //                 parentKeys: ['/hospital/hospitalNotice-list'],
    //             },
    //             {
    //                 name: 'science-detail',
    //                 icon: 'smile',
    //                 path: '/hospital/science-detail',
    //                 hideInMenu: true,
    //                 component: './hospitalNotice/science-detail',
    //                 parentKeys: ['/hospital/hospitalNotice-list'],
    //             },
    //         ],
    //     },
    //     {
    //         path: '/content',
    //         icon: 'profile',
    //         name: 'content',
    //         routes: [
    //             {
    //                 path: '/content',
    //                 redirect: '/content/doctor-creation-manager',
    //             },
    //             {
    //                 name: 'doctor-creation',
    //                 icon: 'smile',
    //                 path: '/content/doctor-creation-manager',
    //                 component: './content/doctor-creation-manager',
    //             },
    //             {
    //                 name: 'doctor-science-detail',
    //                 icon: 'smile',
    //                 path: '/content/science-detail',
    //                 hideInMenu: true,
    //                 component: './content/science-detail',
    //                 parentKeys: ['/content/doctor-creation-manager'],
    //             },
    //             {
    //                 name: 'doctor-creation-detail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/content/doctor-creation-detail',
    //                 component: './content/detail',
    //                 parentKeys: ['/content/doctor-creation'],
    //             },
    //             {
    //                 name: 'creation-edit-detail',
    //                 icon: 'smile',
    //                 path: '/content/creation-edit-detail',
    //                 hideInMenu: true,
    //                 component: './content/editDetail',
    //                 parentKeys: ['/content/doctor-creation'],
    //             }
    //         ],
    //     },
    //     {
    //         path: '/disease',
    //         name: 'disease',
    //         icon: 'profile',
    //         routes: [
    //             {
    //                 path: '/disease',
    //                 redirect: '/disease/disease-list',
    //             },
    //             {
    //                 name: 'list',
    //                 icon: 'smile',
    //                 path: '/disease/disease-list',
    //                 component: './disease/index',
    //             },
    //             {
    //                 name: 'detail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/disease/disease-detail',
    //                 component: './disease/detail',
    //                 parentKeys: ['/disease/disease-list'],
    //             },
    //         ],
    //     },
    //     {
    //         path: '/case',
    //         name: 'case',
    //         icon: 'profile',
    //         routes: [
    //             {
    //                 path: '/case',
    //                 redirect: '/case/case-list',
    //             },
    //             {
    //                 name: 'list',
    //                 icon: 'smile',
    //                 path: '/case/case-list',
    //                 component: './case/index',
    //             },
    //             {
    //                 name: 'detail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/case/case-detail',
    //                 component: './case/detail',
    //                 parentKeys: ['/case/case-list'],
    //             },
    //             {
    //                 name: 'recordDetail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/case/record-detail',
    //                 component: './case/record-detail',
    //                 parentKeys: ['/case/case-list'],
    //             },
    //             {
    //                 name: 'records',
    //                 icon: 'smile',
    //                 path: '/case/records-list',
    //                 component: './caseRecords/index',
    //             },
    //             {
    //                 name: 'recordsDetail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/case/records-detail',
    //                 component: './case/record-detail',
    //                 parentKeys: ['/case/records-list'],
    //             },
    //             {
    //                 name: 'caselist',
    //                 icon: 'smile',
    //                 path: '/case/case-new-list',
    //                 component: './case/list',
    //             },
    //             {
    //                 name: 'caseDetail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/case/case-detail-new',
    //                 component: './case/case-detail-new',
    //                 parentKeys: ['/case/records-list', '/case/case-new-list'],
    //             },
    //         ],
    //     },
    //     {
    //         path: '/experience',
    //         name: 'experience',
    //         icon: 'profile',
    //         routes: [
    //             {
    //                 path: '/experience',
    //                 redirect: '/experience/experience-list',
    //             },
    //             {
    //                 name: 'list',
    //                 icon: 'smile',
    //                 path: '/experience/experience-list',
    //                 component: './experience/index',
    //             },
    //             {
    //                 name: 'detail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/experience/experience-detail',
    //                 component: './experience/detail',
    //                 parentKeys: ['/experience/experience-list'],
    //             },
    //         ],
    //     },
    //     {
    //         path: '/qaa',
    //         name: 'qaa',
    //         icon: 'profile',
    //         routes: [
    //             {
    //                 path: '/qaa',
    //                 redirect: '/qaa/qaa-list',
    //             },
    //             {
    //                 name: 'list',
    //                 icon: 'smile',
    //                 path: '/qaa/qaa-list',
    //                 component: './qaa/index',
    //             },
    //             {
    //                 name: 'detail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/qaa/qaa-detail',
    //                 component: './qaa/detail',
    //                 parentKeys: ['/qaa/qaa-list'],
    //             },
    //         ],
    //     },
    //     {
    //         path: '/article',
    //         name: 'article',
    //         icon: 'profile',
    //         routes: [
    //             {
    //                 path: '/article',
    //                 redirect: '/article/article-list',
    //             },
    //             {
    //                 name: 'list',
    //                 icon: 'smile',
    //                 path: '/article/article-list',
    //                 component: './article/index',
    //             },
    //             {
    //                 name: 'detail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/article/article-detail',
    //                 component: './article/detail',
    //                 parentKeys: ['/article/article-list'],
    //             },
    //         ],
    //     },
    //     {
    //         path: '/activity',
    //         name: 'activity',
    //         icon: 'profile',
    //         routes: [
    //             {
    //                 path: '/activity',
    //                 redirect: '/activity/activity-list',
    //             },
    //             {
    //                 name: 'list',
    //                 icon: 'smile',
    //                 path: '/activity/activity-list',
    //                 component: './activity/index',
    //             },
    //             {
    //                 name: 'detail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/activity/activity-detail',
    //                 component: './activity/detail',
    //                 parentKeys: ['/activity/activity-list'],
    //             },
    //         ],
    //     },
    //     {
    //         path: '/dict',
    //         name: 'dict',
    //         icon: 'profile',
    //         component: './dict',
    //     },
    //     {
    //         path: '/subscriber',
    //         name: 'subscriber',
    //         icon: 'profile',
    //         routes: [
    //             {
    //                 path: '/subscriber',
    //                 redirect: '/subscriber/subscriber-list',
    //             },
    //             {
    //                 name: 'list',
    //                 icon: 'smile',
    //                 path: '/subscriber/subscriber-list',
    //                 component: './subscriber/index',
    //             },
    //             {
    //                 name: 'detail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/subscriber/subscriber-detail',
    //                 component: './subscriber/detail',
    //                 parentKeys: ['/subscriber/subscriber-list'],
    //             },
    //             {
    //                 name: 'userVisitor',
    //                 hideInMenu: true,
    //                 path: '/subscriber/userVisitor',
    //                 component: './subscriber/userVisitor',
    //                 parentKeys: ['/subscriber/subscriber-list'],
    //             },
    //         ],
    //     },
    //     {
    //         name: 'result',
    //         icon: 'CheckCircleOutlined',
    //         path: '/result',
    //         hideInMenu: true,
    //         routes: [
    //             {
    //                 path: '/result',
    //                 redirect: '/result/success',
    //             },
    //             {
    //                 name: 'success',
    //                 icon: 'smile',
    //                 path: '/result/success',
    //                 component: './result/success',
    //             },
    //             {
    //                 name: 'fail',
    //                 icon: 'smile',
    //                 path: '/result/fail',
    //                 component: './result/fail',
    //             },
    //         ],
    //     },
    //     {
    //         name: 'exception',
    //         icon: 'warning',
    //         hideInMenu: true,
    //         path: '/exception',
    //         routes: [
    //             {
    //                 path: '/exception',
    //                 redirect: '/exception/403',
    //             },
    //             {
    //                 name: '403',
    //                 icon: 'smile',
    //                 path: '/exception/403',
    //                 component: './exception/403',
    //             },
    //             {
    //                 name: '404',
    //                 icon: 'smile',
    //                 path: '/exception/404',
    //                 component: './exception/404',
    //             },
    //             {
    //                 name: '500',
    //                 icon: 'smile',
    //                 path: '/exception/500',
    //                 component: './exception/500',
    //             },
    //         ],
    //     },
    //     {
    //         name: 'account',
    //         icon: 'user',
    //         hideInMenu: true,
    //         path: '/account',
    //         routes: [
    //             {
    //                 path: '/account',
    //                 redirect: '/account/center',
    //             },
    //             {
    //                 name: 'center',
    //                 icon: 'smile',
    //                 path: '/account/center',
    //                 component: './account/center',
    //             },
    //             {
    //                 name: 'settings',
    //                 icon: 'smile',
    //                 path: '/account/settings',
    //                 component: './account/settings',
    //             },
    //         ],
    //     },
    //     {
    //         name: 'editor',
    //         icon: 'highlight',
    //         hideInMenu: true,
    //         path: '/editor',
    //         routes: [
    //             {
    //                 path: '/editor',
    //                 redirect: '/editor/flow',
    //             },
    //             {
    //                 name: 'flow',
    //                 icon: 'smile',
    //                 path: '/editor/flow',
    //                 component: './editor/flow',
    //             },
    //             {
    //                 name: 'mind',
    //                 icon: 'smile',
    //                 path: '/editor/mind',
    //                 component: './editor/mind',
    //             },
    //             {
    //                 name: 'koni',
    //                 icon: 'smile',
    //                 path: '/editor/koni',
    //                 component: './editor/koni',
    //             },
    //         ],
    //     },
    //     {
    //         path: '/',
    //         redirect: '/welcome',
    //     },
    //     {
    //         component: '404',
    //     },
    // ];

    // const commonRoutes = [
    //     {
    //         path: '/user',
    //         layout: false,
    //         routes: [
    //             {
    //                 path: '/user/login',
    //                 layout: false,
    //                 name: 'login',
    //                 component: './user/Login',
    //             },
    //             {
    //                 path: '/user',
    //                 redirect: '/user/login',
    //             },
    //             {
    //                 name: 'register-result',
    //                 icon: 'smile',
    //                 path: '/user/register-result',
    //                 component: './user/register-result',
    //             },
    //             {
    //                 name: 'register',
    //                 icon: 'smile',
    //                 path: '/user/register',
    //                 component: './user/register',
    //             },
    //             {
    //                 component: '404',
    //             },
    //         ],
    //     },
    //     {
    //         path: '/welcome',
    //         name: 'welcome',
    //         icon: 'smile',
    //         component: './Welcome',
    //     },
    //     {
    //         path: '/doctor',
    //         name: 'doctor',
    //         icon: 'profile',
    //         routes: [
    //             {
    //                 name: 'authlist',
    //                 icon: 'smile',
    //                 path: '/doctor/auth-list',
    //                 component: './doctor/auth-detail',
    //             },
    //             {
    //                 name: 'authdetail',
    //                 icon: 'smile',
    //                 path: '/doctor/auth-detail',
    //                 component: './doctor/auth-detail',
    //             },
    //         ],
    //     },
    //     {
    //         path: '/hospital',
    //         icon: 'profile',
    //         name: 'hospital',
    //         routes: [
    //             {
    //                 path: '/hospital',
    //                 redirect: '/hospital/hospital-list',
    //             },
    //             {
    //                 name: 'list',
    //                 icon: 'smile',
    //                 path: '/hospital/hospital-list',
    //                 component: './hospital/index',
    //             },
    //             {
    //                 name: 'authlist',
    //                 icon: 'smile',
    //                 path: '/hospital/authList',
    //                 component: './hospital/authList',
    //             },
    //             {
    //                 name: 'detail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/hospital/hospital-detail',
    //                 parentKeys: ['/hospital/hospital-list'],
    //                 component: './hospital/detail',
    //             },
    //             {
    //                 name: 'hospitalDeptList',
    //                 icon: 'smile',
    //                 path: '/hospital/hospitalDept-list',
    //                 component: './hospitalDept/index',
    //             },
    //             {
    //                 name: 'hospitalDeptDetail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/hospital/hospitalDept-detail',
    //                 component: './hospitalDept/detail',
    //                 parentKeys: ['/hospital/hospitalDept-list'],
    //             },
    //             {
    //                 name: 'hospitalNoticeList',
    //                 icon: 'smile',
    //                 path: '/hospital/hospitalNotice-list',
    //                 component: './hospitalNotice/index',
    //             },
    //             {
    //                 name: 'hospitalNoticeDetail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/hospital/hospitalNotice-detail',
    //                 component: './hospitalNotice/detail',
    //                 parentKeys: ['/hospital/hospitalNotice-list'],
    //             },
    //         ],
    //     },
    //     {
    //         path: '/content',
    //         icon: 'profile',
    //         name: 'content',
    //         routes: [
    //             {
    //                 path: '/content',
    //                 redirect: '/content/doctor-creation',
    //             },
    //             {
    //                 name: 'doctor-creation',
    //                 icon: 'smile',
    //                 path: '/content/doctor-creation',
    //                 component: './content/doctor-creation',
    //             },
    //         ],
    //     },
    //     {
    //         path: '/disease',
    //         name: 'disease',
    //         icon: 'profile',
    //         routes: [
    //             {
    //                 path: '/disease',
    //                 redirect: '/disease/disease-list',
    //             },
    //             {
    //                 name: 'list',
    //                 icon: 'smile',
    //                 path: '/disease/disease-list',
    //                 component: './disease/index',
    //             },
    //             {
    //                 name: 'detail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/disease/disease-detail',
    //                 component: './disease/detail',
    //                 parentKeys: ['/disease/disease-list'],
    //             },
    //         ],
    //     },
    //     {
    //         path: '/case',
    //         name: 'case',
    //         icon: 'profile',
    //         routes: [
    //             {
    //                 path: '/case',
    //                 redirect: '/case/case-list',
    //             },
    //             {
    //                 name: 'list',
    //                 icon: 'smile',
    //                 path: '/case/case-list',
    //                 component: './case/index',
    //             },
    //             {
    //                 name: 'detail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/case/case-detail',
    //                 component: './case/detail',
    //                 parentKeys: ['/case/case-list'],
    //             },
    //             {
    //                 name: 'recordDetail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/case/record-detail',
    //                 component: './case/record-detail',
    //                 parentKeys: ['/case/case-list'],
    //             },
    //             {
    //                 name: 'records',
    //                 icon: 'smile',
    //                 path: '/case/records-list',
    //                 component: './caseRecords/index',
    //             },
    //             {
    //                 name: 'recordsDetail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/case/records-detail',
    //                 component: './case/record-detail',
    //                 parentKeys: ['/case/records-list'],
    //             },
    //             {
    //                 name: 'caselist',
    //                 icon: 'smile',
    //                 path: '/case/case-new-list',
    //                 component: './case/list',
    //             },
    //             {
    //                 name: 'caseDetail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/case/case-detail-new',
    //                 component: './case/case-detail-new',
    //                 parentKeys: ['/case/records-list', '/case/case-new-list'],
    //             },
    //         ],
    //     },
    //     {
    //         path: '/experience',
    //         name: 'experience',
    //         icon: 'profile',
    //         routes: [
    //             {
    //                 path: '/experience',
    //                 redirect: '/experience/experience-list',
    //             },
    //             {
    //                 name: 'list',
    //                 icon: 'smile',
    //                 path: '/experience/experience-list',
    //                 component: './experience/index',
    //             },
    //             {
    //                 name: 'detail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/experience/experience-detail',
    //                 component: './experience/detail',
    //                 parentKeys: ['/experience/experience-list'],
    //             },
    //         ],
    //     },
    //     {
    //         path: '/qaa',
    //         name: 'qaa',
    //         icon: 'profile',
    //         routes: [
    //             {
    //                 path: '/qaa',
    //                 redirect: '/qaa/qaa-list',
    //             },
    //             {
    //                 name: 'list',
    //                 icon: 'smile',
    //                 path: '/qaa/qaa-list',
    //                 component: './qaa/index',
    //             },
    //             {
    //                 name: 'detail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/qaa/qaa-detail',
    //                 component: './qaa/detail',
    //                 parentKeys: ['/qaa/qaa-list'],
    //             },
    //         ],
    //     },
    //     {
    //         path: '/article',
    //         name: 'article',
    //         icon: 'profile',
    //         routes: [
    //             {
    //                 path: '/article',
    //                 redirect: '/article/article-list',
    //             },
    //             {
    //                 name: 'list',
    //                 icon: 'smile',
    //                 path: '/article/article-list',
    //                 component: './article/index',
    //             },
    //             {
    //                 name: 'detail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/article/article-detail',
    //                 component: './article/detail',
    //                 parentKeys: ['/article/article-list'],
    //             },
    //         ],
    //     },
    //     {
    //         path: '/activity',
    //         name: 'activity',
    //         icon: 'profile',
    //         routes: [
    //             {
    //                 path: '/activity',
    //                 redirect: '/activity/activity-list',
    //             },
    //             {
    //                 name: 'list',
    //                 icon: 'smile',
    //                 path: '/activity/activity-list',
    //                 component: './activity/index',
    //             },
    //             {
    //                 name: 'detail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/activity/activity-detail',
    //                 component: './activity/detail',
    //                 parentKeys: ['/activity/activity-list'],
    //             },
    //         ],
    //     },
    //     {
    //         path: '/dict',
    //         name: 'dict',
    //         icon: 'profile',
    //         component: './dict',
    //     },
    //     {
    //         path: '/subscriber',
    //         name: 'subscriber',
    //         icon: 'profile',
    //         routes: [
    //             {
    //                 path: '/subscriber',
    //                 redirect: '/subscriber/subscriber-list',
    //             },
    //             {
    //                 name: 'list',
    //                 icon: 'smile',
    //                 path: '/subscriber/subscriber-list',
    //                 component: './subscriber/index',
    //             },
    //             {
    //                 name: 'detail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/subscriber/subscriber-detail',
    //                 component: './subscriber/detail',
    //                 parentKeys: ['/subscriber/subscriber-list'],
    //             },
    //             {
    //                 name: 'userVisitor',
    //                 hideInMenu: true,
    //                 path: '/subscriber/userVisitor',
    //                 component: './subscriber/userVisitor',
    //                 parentKeys: ['/subscriber/subscriber-list'],
    //             },
    //         ],
    //     },
    //     {
    //         name: 'result',
    //         icon: 'CheckCircleOutlined',
    //         path: '/result',
    //         hideInMenu: true,
    //         routes: [
    //             {
    //                 path: '/result',
    //                 redirect: '/result/success',
    //             },
    //             {
    //                 name: 'success',
    //                 icon: 'smile',
    //                 path: '/result/success',
    //                 component: './result/success',
    //             },
    //             {
    //                 name: 'fail',
    //                 icon: 'smile',
    //                 path: '/result/fail',
    //                 component: './result/fail',
    //             },
    //         ],
    //     },
    //     {
    //         name: 'exception',
    //         icon: 'warning',
    //         hideInMenu: true,
    //         path: '/exception',
    //         routes: [
    //             {
    //                 path: '/exception',
    //                 redirect: '/exception/403',
    //             },
    //             {
    //                 name: '403',
    //                 icon: 'smile',
    //                 path: '/exception/403',
    //                 component: './exception/403',
    //             },
    //             {
    //                 name: '404',
    //                 icon: 'smile',
    //                 path: '/exception/404',
    //                 component: './exception/404',
    //             },
    //             {
    //                 name: '500',
    //                 icon: 'smile',
    //                 path: '/exception/500',
    //                 component: './exception/500',
    //             },
    //         ],
    //     },
    //     {
    //         name: 'editor',
    //         icon: 'highlight',
    //         hideInMenu: true,
    //         path: '/editor',
    //         routes: [
    //             {
    //                 path: '/editor',
    //                 redirect: '/editor/flow',
    //             },
    //             {
    //                 name: 'flow',
    //                 icon: 'smile',
    //                 path: '/editor/flow',
    //                 component: './editor/flow',
    //             },
    //             {
    //                 name: 'mind',
    //                 icon: 'smile',
    //                 path: '/editor/mind',
    //                 component: './editor/mind',
    //             },
    //             {
    //                 name: 'koni',
    //                 icon: 'smile',
    //                 path: '/editor/koni',
    //                 component: './editor/koni',
    //             },
    //         ],
    //     },
    //     {
    //         path: '/',
    //         redirect: '/welcome',
    //     },
    //     {
    //         component: '404',
    //     },
    // ];

    // const doctorRoutes = [
    //     {
    //         path: '/user',
    //         layout: false,
    //         routes: [
    //             {
    //                 path: '/user/login',
    //                 layout: false,
    //                 name: 'login',
    //                 component: './user/Login',
    //             },
    //             {
    //                 path: '/user',
    //                 redirect: '/user/login',
    //             },
    //             {
    //                 name: 'register-result',
    //                 icon: 'smile',
    //                 path: '/user/register-result',
    //                 component: './user/register-result',
    //             },
    //             {
    //                 name: 'register',
    //                 icon: 'smile',
    //                 path: '/user/register',
    //                 component: './user/register',
    //             },
    //             {
    //                 component: '404',
    //             },
    //         ],
    //     },
    //     {
    //         path: '/welcome',
    //         name: 'welcome',
    //         icon: 'smile',
    //         component: './Welcome',
    //     },
    //     {
    //         name: 'doctor.composition',
    //         icon: 'smile',
    //         path: '/content/doctor-creation',
    //         component: './content/doctor-creation',
    //     },
    //     {
    //         name: 'content.doctor-creation-detail',
    //         icon: 'smile',
    //         hideInMenu: true,
    //         path: '/content/doctor-creation-detail',
    //         component: './content/detail',
    //         parentKeys: ['/content/doctor-creation'],
    //     },
    //     {
    //         path: '/publish',
    //         name: 'publish',
    //         icon: 'profile',
    //         routes: [
    //             {
    //                 path: '/publish',
    //                 redirect: '/publish/science',
    //             },
    //             {
    //                 name: 'science',
    //                 icon: 'smile',
    //                 path: '/publish/science',
    //                 component: './science/index',
    //             },
    //             {
    //                 name: 'sciencedetail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/publish/sciencedetail',
    //                 component: './science/science-detail',
    //                 parentKeys: ['/publish/science'],
    //             },
    //             {
    //                 name: 'dynamic',
    //                 icon: 'smile',
    //                 path: '/publish/dynamic',
    //                 component: './dynamic/index',
    //             },
    //             {
    //                 name: 'dynamicdetail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/publish/dynamicdetail',
    //                 component: './dynamic/dynamic-detail',
    //                 parentKeys: ['/publish/dynamic'],
    //             },
    //         ],
    //     },
    //     {
    //         path: '/setting',
    //         name: 'setting',
    //         icon: 'profile',
    //         routes: [
    //             {
    //                 path: '/setting',
    //                 redirect: '/setting/hospital',
    //             },
    //             {
    //                 name: 'hospital',
    //                 icon: 'smile',
    //                 path: '/setting/hospital',
    //                 component: './setting/hospital-detail',
    //             },
    //             {
    //                 name: 'hospitalDeptList',
    //                 icon: 'smile',
    //                 path: '/hospital/dept-list',
    //                 component: './hospitalDept/list',
    //             },
    //             {
    //                 name: 'hospitalDeptDetail',
    //                 icon: 'smile',
    //                 hideInMenu: true,
    //                 path: '/hospital/dept-detail',
    //                 component: './hospitalDept/dept-detail',
    //                 parentKeys: ['/hospital/dept-list'],
    //             },
    //             {
    //                 name: 'accountSettings',
    //                 icon: 'smile',
    //                 path: '/setting/account',
    //                 component: './setting/account',
    //             }
    //         ],
    //     },
    {
        path: '/',
        redirect: '/welcome',
    },
    {
        component: '404',
    },
];

let defaultRoutes = [
    {
        path: '/user',
        layout: false,
        routes: [
            {
                path: '/user/login',
                layout: false,
                name: 'login',
                component: './user/Login',
            },
            {
                path: '/user',
                redirect: '/user/login',
            },
            {
                name: 'register-result',
                icon: 'smile',
                path: '/user/register-result',
                component: './user/register-result',
            },
            {
                name: 'register',
                icon: 'smile',
                path: '/user/register',
                component: './user/register',
            },
            {
                component: '404',
            },
        ],
    },
    {
        path: '/welcome',
        name: 'welcome',
        icon: 'smile',
        component: './Welcome',
    },
    {
        path: '/',
        redirect: '/welcome',
    },
    {
        component: './404',
    },
];
// ProLayout 支持的api https://procomponents.ant.design/components/layout
export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) => {
    return {
        menuDataRender: () => {
            if (initialState?.currentUser) {
                console.log('initialState.currentUser.adminType', initialState.currentUser.adminType);
                let routes = [];
                if (initialState.currentUser.adminType == 0) routes = loopMenuItem(adminRoutes);
                else if (initialState.currentUser.adminType == 1) routes = loopMenuItem(adminRoutes);
                else routes = loopMenuItem(adminRoutes);
                //  console.log('imenuDataRender', routes);
                return routes;
            }
            return loopMenuItem(defaultRoutes);
        },
        // menuDataRender: undefined,
        rightContentRender: () => <RightContent />,
        disableContentMargin: false,
        waterMarkProps: {
            content: initialState?.currentUser?.name,
        },
        footerRender: () => <Footer />,
        onPageChange: () => {
            const { location } = history;
            // 如果没有登录，重定向到 login
            if (
                !initialState?.currentUser &&
                location.pathname !== loginPath &&
                location.pathname !== '/user/register'
            ) {
                history.push(loginPath);
            }
        },
        links: [],
        menuHeaderRender: undefined,

        // 自定义 403 页面
        // unAccessible: <div>unAccessible</div>,
        // 增加一个 loading 的状态
        childrenRender: (children, props) => {
            // if (initialState?.loading) return <PageLoading />;
            return (
                <>
                    {children}
                    {!props.location?.pathname?.includes('/login') && (
                        <SettingDrawer
                            disableUrlParams
                            enableDarkTheme
                            settings={initialState?.settings}
                            onSettingChange={(settings) => {
                                setInitialState((preInitialState) => ({
                                    ...preInitialState,
                                    settings,
                                }));
                            }}
                        />
                    )}
                </>
            );
        },
        ...initialState?.settings,
    };
};

/**
 * 异常处理程序
    200: '服务器成功返回请求的数据。',
    201: '新建或修改数据成功。',
    202: '一个请求已经进入后台排队（异步任务）。',
    204: '删除数据成功。',
    400: '发出的请求有错误，服务器没有进行新建或修改数据的操作。',
    401: '用户没有权限（令牌、用户名、密码错误）。',
    403: '用户得到授权，但是访问是被禁止的。',
    404: '发出的请求针对的是不存在的记录，服务器没有进行操作。',
    405: '请求方法不被允许。',
    406: '请求的格式不可得。',
    410: '请求的资源被永久删除，且不会再得到的。',
    422: '当创建一个对象时，发生一个验证错误。',
    500: '服务器发生错误，请检查服务器。',
    502: '网关错误。',
    503: '服务不可用，服务器暂时过载或维护。',
    504: '网关超时。',
 //-----English
    200: The server successfully returned the requested data. ',
    201: New or modified data is successful. ',
    202: A request has entered the background queue (asynchronous task). ',
    204: Data deleted successfully. ',
    400: 'There was an error in the request sent, and the server did not create or modify data. ',
    401: The user does not have permission (token, username, password error). ',
    403: The user is authorized, but access is forbidden. ',
    404: The request sent was for a record that did not exist. ',
    405: The request method is not allowed. ',
    406: The requested format is not available. ',
    410':
        'The requested resource is permanently deleted and will no longer be available. ',
    422: When creating an object, a validation error occurred. ',
    500: An error occurred on the server, please check the server. ',
    502: Gateway error. ',
    503: The service is unavailable. ',
    504: The gateway timed out. ',
 * @see https://beta-pro.ant.design/docs/request-cn
 */

const authHeaderInterceptor = (url: string, options: RequestConfig) => {
    // const authHeader = {
    //     access_token: sessionStorage.getItem('currentUser')
    //         ? JSON.parse(sessionStorage.getItem('currentUser')).token
    //         : '',
    // };
    // return {
    //     url: `${url}`,
    //     options: { ...options, interceptors: true, headers: authHeader },
    // };
};

// export const request: RequestConfig = {
//   // 新增自动添加AccessToken的请求前拦截器
//   requestInterceptors: [authHeaderInterceptor],
// };

export const request = {
    errorHandler: (error) => {
        const { response } = error;
        console.log('response', response);
        if (response) {
            const data = error.data;

            switch (response.status) {
                case 403:
                    notification.error({ message: '系统提示', description: '拒绝访问', duration: 4 });
                    break;
                case 500:
                    // const type = response.request.responseType;
                    // if (type === 'blob') {
                    //   blobToJson(data);
                    //   break;
                    // }
                    console.log('500');
                    if (data.message.includes('失效') || data.message.includes('权限')) {
                        Modal.error({
                            title: '登录已过期',
                            content: '很抱歉，登录已过期，请重新登录',
                            okText: '重新登录',
                            mask: false,
                            onOk: () => {
                                sessionStorage.clear();
                                window.location.href = '/user/login';
                            },
                        });
                    } else {
                        notification.error({ message: '系统提示', description: data.message, duration: 4 });
                    }
                    break;
                case 404:
                    notification.error({
                        message: '系统提示',
                        description: '很抱歉，资源未找到!',
                        duration: 4,
                    });
                    break;
                case 504:
                    notification.error({ message: '系统提示', description: '网络超时' });
                    break;
                case 401:
                    notification.error({
                        message: '系统提示',
                        description: '未授权，请重新登录',
                        duration: 4,
                    });
                    sessionStorage.clear();
                    window.location.href = '/user/login';
                    break;
                default:
                    notification.error({
                        message: '系统提示',
                        description: data.msg,
                        duration: 4,
                    });
                    break;
            }
        }

        throw error;
    },
    // headers: {
    //   Authorization: `Basic ${Base64.encode(`operation:operation`)}`,
    //   'mate-tenant-id': 'operation',
    // },
    requestInterceptors: [authHeaderInterceptor],
    // 新增自动添加AccessToken的请求前拦截器
    //instanceRequestInterceptors: [authHeaderInterceptor],
};
